<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
// load base class if needed
require_once( APPPATH . 'controllers/base/OperatorBase.php' );

class barang extends ApplicationBase {

    // constructor
    public function __construct() {
        parent::__construct();
        // load model
        $this->load->model('m_barang');
        $this->load->model('m_group_barang');
        $this->load->model('m_kategoribarang');
        $this->load->model('m_jenisbarang');
        $this->load->model('m_merk');
        $this->load->model('m_lokasi');
        $this->load->model('m_ruang');
        $this->load->model('m_vendor');
        $this->load->model('m_pengajuan_barang');

        // load library
        $this->load->library('tnotification');
        $this->load->library('pagination');

        // load helpers
//        $this->smarty->load_javascript("resource/js/jquery/jquery-ui-1.9.2.custom.min.js");
//        $this->smarty->load_style("jquery.ui/redmond/jquery-ui-1.8.13.custom.css");
        $this->smarty->load_javascript("resource/js/autonumeric/autonumeric.js");
        
        //select 2
        $this->smarty->load_javascript("resource/js/select2/select2.js");
        $this->smarty->load_style("select2/select2.css");
    }

    // welcome administrator
    public function index() {
        // set template content
        $this->smarty->assign("template_content", "master/barang/list.html");

        //pencarian
        $search = $this->tsession->userdata('master_barang_search');
        $this->smarty->assign("search", $search);
        $barang_noinventaris = empty($search['barang_noinventaris']) ? '%' : '%' .$search['barang_noinventaris'].'%' ;
        $barang_nama = empty($search['barang_nama']) ? '%' : '%' .$search['barang_nama'].'%';
        $barang_kondisi = empty($search['barang_kondisi']) ? '%' : $search['barang_kondisi'];
        $barang_status = empty($search['barang_status']) ? '%' : $search['barang_status'];
        $gpbarang_cd = empty($search['gpbarang_cd']) ? '%' : $search['gpbarang_cd'];
        $jenisbarang_cd = empty($search['jenisbarang_cd']) ? '%' : $search['jenisbarang_cd'];
        $kategoribarang_cd = empty($search['kategoribarang_cd']) ? '%' : $search['kategoribarang_cd'];
        $merk_cd = empty($search['merk_cd']) ? '%' : $search['merk_cd'];
        
        $params = array(
            $barang_noinventaris,
            $barang_nama, 
            $barang_kondisi, 
            $barang_status, 
            $gpbarang_cd, 
            $jenisbarang_cd, 
            $kategoribarang_cd, 
            $merk_cd);

        /* start of pagination --------------------- */
        // pagination
        $config['base_url'] = site_url("master/barang/index/");
        $config['total_rows'] = $this->m_barang->get_count_all_barang_search($params);
        $config['uri_segment'] = 4;
        $config['per_page'] = 15;
        $this->pagination->initialize($config);
        $pagination['data'] = $this->pagination->create_links();
        // pagination attribute
        $start = $this->uri->segment(4, 0) + 1;
        $end = $this->uri->segment(4, 0) + $config['per_page'];
        $end = (($end > $config['total_rows']) ? $config['total_rows'] : $end);
        $pagination['start'] = ($config['total_rows'] == 0) ? 0 : $start;
        $pagination['end'] = $end;
        $pagination['total'] = $config['total_rows'];
        // pagination assign value
        $this->smarty->assign("pagination", $pagination);
        $this->smarty->assign("no", $start);
        /* end of pagination ---------------------- */
        // get data
        $params = array($barang_noinventaris,
            $barang_nama, 
            $barang_kondisi, 
            $barang_status, 
            $gpbarang_cd, 
            $jenisbarang_cd, 
            $kategoribarang_cd, 
            $merk_cd,
            ($start - 1), 
            $config['per_page']);
        
        $this->smarty->assign("rs_id", $this->m_barang->get_all_barang_pagination_search($params));

        //data
        $rs_kategoribarang = $this->m_kategoribarang->get_all_kategoribarang();
        $this->smarty->assign("rs_kategoribarang", $rs_kategoribarang);

        $rs_gpbarang = $this->m_group_barang->get_all_group_barang();
        $this->smarty->assign("rs_gpbarang", $rs_gpbarang);

        $rs_jenisbarang = $this->m_jenisbarang->get_all_jenisbarang();
        $this->smarty->assign("rs_jenisbarang", $rs_jenisbarang);

        $rs_merk = $this->m_merk->get_all_merk();
        $this->smarty->assign("rs_merk", $rs_merk);

        $barang_kondisi = Array(
            Array("data" => "baik"),
            Array("data" => "rusak")
        );

        $barang_status = Array(
            Array('status_cd' => 'digunakan', 'status_name' => 'di Gunakan'),
            Array('status_cd' => 'digudang', 'status_name' => 'di Gudang'),
            Array('status_cd' => 'disewakan', 'status_name' => 'di Sewakan'),
            Array('status_cd' => 'dipinjamkan', 'status_name' => 'di Pinjamkan'),
            Array('status_cd' => 'service', 'status_name' => 'Service')
        );
        $this->smarty->assign("barang_status", $barang_status);
        $this->smarty->assign("barang_kondisi", $barang_kondisi);

        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }

    public function proses_cari() {
        // set page rules
        $this->_set_page_rule("R");
        // data
        if ($this->input->post('save') == "Reset") {
            $this->tsession->unset_userdata('master_barang_search');
        } else {
            $params = array(
                "barang_noinventaris" => $this->input->post("barang_noinventaris"),
                "barang_nama" => $this->input->post("barang_nama"),
                "barang_kondisi" => $this->input->post("barang_kondisi"),
                "barang_status" => $this->input->post("barang_status"),
                "gpbarang_cd" => $this->input->post("gpbarang_cd"),
                "jenisbarang_cd" => $this->input->post("jenisbarang_cd"),
                "kategoribarang_cd" => $this->input->post("kategoribarang_cd"),
                "merk_cd" => $this->input->post("merk_cd")
            );
            
            $this->tsession->set_userdata("master_barang_search", $params);
            //print_r($params);
            //exit();
        }
        // redirect
        redirect("master/barang");
    }

    public function add() {
        // $this->_set_page_rule("R");
        // set template content
        $this->smarty->assign("template_content", "master/barang/add.html");

        //data
        $rs_kategoribarang = $this->m_kategoribarang->get_all_kategoribarang();
        $this->smarty->assign("rs_kategoribarang", $rs_kategoribarang);

        $rs_gpbarang = $this->m_group_barang->get_all_group_barang();
        $this->smarty->assign("rs_gpbarang", $rs_gpbarang);

        $rs_jenisbarang = $this->m_jenisbarang->get_all_jenisbarang();
        $this->smarty->assign("rs_jenisbarang", $rs_jenisbarang);

        $rs_merk = $this->m_merk->get_all_merk();
        $this->smarty->assign("rs_merk", $rs_merk);

        $rs_lokasi = $this->m_lokasi->get_all_lokasi();
        $this->smarty->assign("rs_lokasi", $rs_lokasi);
        
        $rs_ruang = $this->m_ruang->get_all_ruang();
        $this->smarty->assign("rs_ruang", $rs_ruang);
        
        $rs_vendor = $this->m_vendor->get_all_vendor();
        $this->smarty->assign("rs_vendor", $rs_vendor);

        $barang_kondisi = Array(
            Array("data" => "baik"),
            Array("data" => "rusak")
        );

        $barang_status = Array(
            Array("data" => "digunakan"),
            Array("data" => "digudang"),
            Array("data" => "disewakan"),
            Array("data" => "dipinjamkan"),
            Array("data" => "service")
        );
        $this->smarty->assign("barang_status", $barang_status);
        $this->smarty->assign("barang_kondisi", $barang_kondisi);

        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }

    // welcome administrator
    public function view_barang($id) {
        $this->smarty->load_javascript("resource/js/jquery/jquery-ui-1.9.2.custom.min.js");
        $this->smarty->load_style("jquery.ui/redmond/jquery-ui-1.8.13.custom.css");

        $bulan = array(
            "01" => "Januari",
            "02" => "Februari",
            "03" => "Maret",
            "04" => "April",
            "05" => "Mei",
            "06" => "Juni",
            "07" => "Juli",
            "08" => "Agustus",
            "09" => "September",
            "10" => "Oktober",
            "11" => "November",
            "12" => "Desember");

        $data = $this->m_barang->get_barang_detail_by_id($id);
        /*
          $html = '<div id="tabs">
          <ul>
          <li><a href="#tabs-1">Nunc tincidunt</a></li>
          <li><a href="#tabs-2">Proin dolor</a></li>
          <li><a href="#tabs-3">Aenean lacinia</a></li>
          </ul>
          <div id="tabs-1">
          <p>Proin elit arcu, rutrum commodo, vehicula tempus, commodo a, risus. Curabitur nec arcu. Donec sollicitudin mi sit amet mauris. Nam elementum quam ullamcorper ante. Etiam aliquet massa et lorem. Mauris dapibus lacus auctor risus. Aenean tempor ullamcorper leo. Vivamus sed magna quis ligula eleifend adipiscing. Duis orci. Aliquam sodales tortor vitae ipsum. Aliquam nulla. Duis aliquam molestie erat. Ut et mauris vel pede varius sollicitudin. Sed ut dolor nec orci tincidunt interdum. Phasellus ipsum. Nunc tristique tempus lectus.</p>
          </div>
          <div id="tabs-2">
          <p>Morbi tincidunt, dui sit amet facilisis feugiat, odio metus gravida ante, ut pharetra massa metus id nunc. Duis scelerisque molestie turpis. Sed fringilla, massa eget luctus malesuada, metus eros molestie lectus, ut tempus eros massa ut dolor. Aenean aliquet fringilla sem. Suspendisse sed ligula in ligula suscipit aliquam. Praesent in eros vestibulum mi adipiscing adipiscing. Morbi facilisis. Curabitur ornare consequat nunc. Aenean vel metus. Ut posuere viverra nulla. Aliquam erat volutpat. Pellentesque convallis. Maecenas feugiat, tellus pellentesque pretium posuere, felis lorem euismod felis, eu ornare leo nisi vel felis. Mauris consectetur tortor et purus.</p>
          </div>
          <div id="tabs-3">
          <p>Mauris eleifend est et turpis. Duis id erat. Suspendisse potenti. Aliquam vulputate, pede vel vehicula accumsan, mi neque rutrum erat, eu congue orci lorem eget lorem. Vestibulum non ante. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Fusce sodales. Quisque eu urna vel enim commodo pellentesque. Praesent eu risus hendrerit ligula tempus pretium. Curabitur lorem enim, pretium nec, feugiat nec, luctus a, lacus.</p>
          <p>Duis cursus. Maecenas ligula eros, blandit nec, pharetra at, semper at, magna. Nullam ac lacus. Nulla facilisi. Praesent viverra justo vitae neque. Praesent blandit adipiscing velit. Suspendisse potenti. Donec mattis, pede vel pharetra blandit, magna ligula faucibus eros, id euismod lacus dolor eget odio. Nam scelerisque. Donec non libero sed nulla mattis commodo. Ut sagittis. Donec nisi lectus, feugiat porttitor, tempor ac, tempor vitae, pede. Aenean vehicula velit eu tellus interdum rutrum. Maecenas commodo. Pellentesque nec elit. Fusce in lacus. Vivamus a libero vitae lectus hendrerit hendrerit.</p>
          </div>
          </div>';
         */
        $html = '<table class="table-view" id="table-list-pengadaan">';
        $html .= '<tbody>';
        $html .= '<tr>
                    <th align="left" width="20%">&nbsp;  No Inventaris</th>
                    <td>' . $data['barang_noinventaris'] . '</td>
                 </tr>';
        $html .= '<tr>
                    <th align="left">&nbsp;  Nama</th>
                    <td>' . $data['barang_nama'] . '</td>
                 </tr>';
        $html .= '<tr>
                    <th align="left">&nbsp; Harga beli</th>
                    <td>' . number_format($data['barang_harga_beli'], 2) . '</td>
                 </tr>';
        $html .= '<tr>
                    <th align="left">&nbsp; Jenis Barang</th>
                    <td>' . $data['jenisbarang_name'] . '</td>
                 </tr>';
        $html .= '<tr>
                    <th align="left">&nbsp; Kategori</th>
                    <td>' . $data['kategoribarang_name'] . '</td>
                 </tr>';
        $html .= '<tr>
                    <th align="left">&nbsp; Merk</th>
                    <td>' . $data['merk_name'] . '</td>
                 </tr>';
        $html .= '<tr>
                    <th align="left">&nbsp; Lokasi</th>
                    <td>' . $data['lokasi_name'] . '</td>
                 </tr>';
        $html .= '<tr>
                    <th align="left">&nbsp; Group</th>
                    <td>' . $data['gpbarang_name'] . '</td>
                 </tr>';

        $tanggal = explode('-', $data['barang_tglbeli']);
        if ($data['barang_tglbeli'] == "0000-00-00") {
            $tgl_detail = "-";
        } else {
            $tgl_detail = $tanggal[0] . ' ' . $bulan[$tanggal[1]] . ' ' . $tanggal[2];
        }

        $html .= '<tr>
                    <th align="left">&nbsp; Tanggal Beli</th>
                    <td>' . $tgl_detail . '</td>
                 </tr>';

        $html .='</tbody>';
        $html .= '</table>';
        echo $html;
    }

    // add process
    public function add_process() {
        // cek input 
        $this->tnotification->set_rules('barang_noinventaris', 'No Inventaris Barang', 'trim|required|max_length[30]');
        $this->tnotification->set_rules('barang_nama', 'Nama Barang', 'trim|required|max_length[100]');
        $this->tnotification->set_rules('barang_harga_beli', 'Harga Barang', 'trim|required|max_length[100]');
        $this->tnotification->set_rules('barang_nilai_buku', 'Nilai Buku', 'trim|required|max_length[100]');
        $this->tnotification->set_rules('barang_umurekonomis', 'Umur Ekonomis', 'trim|required|max_length[2]');

        $this->tnotification->set_rules('gpbarang_cd', 'Group Barang', 'trim|required');
        $this->tnotification->set_rules('jenisbarang_cd', 'Jenis Barang', 'trim|required');
        $this->tnotification->set_rules('merk_cd', 'Merk Barang', 'trim|required');
        $this->tnotification->set_rules('kategoribarang_cd', 'Kategori Barang', 'trim|required');

        $barang_susut = $this->input->post('barang_susut');
        $barang_susut = empty($barang_susut) ? '0' : $barang_susut;
        $barang_garansi = $this->input->post('barang_garansi');
        $barang_garansi = empty($barang_garansi) ? '0' : $barang_garansi;
 
        //set parameter
        $params = array(
            'barang_noinventaris'       => '',
            'barang_nama'               => '',
            'barang_harga_beli'         => '', 
            'barang_nilai_buku'         => '',
            'barang_nilai_upgrade'      => '',
            'barang_umurekonomis'       => '',
            'barang_keterangan'         => '',
            'barang_kondisi'            => '',
            'barang_status'             => '',
            'barang_tglbeli'            => '',
            'barang_tglberakhir'        => '', 
            'barang_tglpenyusutan'      => '', 
            'barang_garansi'            => '',
            'barang_tgl_garansi'        => '', 
            'barang_pengguna'           => '',
            'barang_susut'              => '',
            'barang_id_lama'            => null,
            'barang_id_baru'            => null,
            'barang_upgrade'            => null,
            'gpbarang_cd'               => '',
            'vendor_cd'                 => '',
            'lokasi_cd'                 => '',
            'ruang_cd'                  => '',
            'kategoribarang_cd'         => '', 
            'jenisbarang_cd'            => '', 
            'merk_cd'                   => '',
            'pengadaan_id'              => null,
            'pengajuanbarang_id'        => null,
            'mdb'                       => $this->com_user['user_id']
        );
        $params = array(
            $this->input->post('barang_noinventaris'),
            $this->input->post('barang_nama'),
            str_replace(".", "", $this->input->post('barang_harga_beli')),
            str_replace(".", "", $this->input->post('barang_nilai_buku')),
            $this->input->post('barang_umurekonomis'),
            $this->input->post('barang_keterangan'),
            $this->input->post('barang_kondisi'),
            $this->input->post('barang_status'),
            $this->input->post('barang_tglbeli'),
            null,
            $barang_garansi,
            $this->input->post('barang_tgl_garansi'),
            null,
            $barang_susut,
            $this->input->post('gpbarang_cd'),
            $this->input->post('vendor_cd'),
            $this->input->post('lokasi_cd'),
            $this->input->post('ruang_cd'),
            $this->input->post('kategoribarang_cd'),
            $this->input->post('jenisbarang_cd'),
            $this->input->post('merk_cd'),
            null,
            null,
            $this->com_user['user_id']
        );

        //print_r($params);     
        //cek data
        $jum_check_kode = $this->m_barang->get_count_all_barang_by_kode($params);
        $jum_check_kode_name = $this->m_barang->get_count_all_barang_by_kode_nama($params);

        // process
        if ($jum_check_kode > 0) {
            $this->tnotification->sent_notification("error", "Kombinasi Kode dan Nama sudah digunakan");
        } else if ($this->tnotification->run() !== FALSE) {
            // insert            
            if ($this->m_barang->insert($params)) {
                // notification
                $this->tnotification->delete_last_field();
                $this->tnotification->sent_notification("success", "Data berhasil disimpan");
            } else {
                // default error
                $this->tnotification->sent_notification("error", "Data gagal disimpan");
            }
        } else {
            // default error
            $this->tnotification->sent_notification("error", "Data gagal disimpan");
        }
        // default redirect
        redirect("master/barang/add");
    }

    public function edit($id) {
        // $this->_set_page_rule("R");
        // set template content
        $this->smarty->assign("template_content", "master/barang/edit.html");

        //data
        $result = $this->m_barang->get_barang_by_id($id);
        $this->smarty->assign("result", $result);

        $rs_kategoribarang = $this->m_kategoribarang->get_all_kategoribarang();
        $this->smarty->assign("rs_kategoribarang", $rs_kategoribarang);

        $rs_gpbarang = $this->m_group_barang->get_all_group_barang();
        $this->smarty->assign("rs_gpbarang", $rs_gpbarang);

        $rs_jenisbarang = $this->m_jenisbarang->get_all_jenisbarang();
        $this->smarty->assign("rs_jenisbarang", $rs_jenisbarang);

        $rs_merk = $this->m_merk->get_all_merk();
        $this->smarty->assign("rs_merk", $rs_merk);


        $rs_lokasi = $this->m_lokasi->get_all_lokasi();
        $this->smarty->assign("rs_lokasi", $rs_lokasi);
        
        $rs_ruang = $this->m_ruang->get_all_ruang();
        $this->smarty->assign("rs_ruang", $rs_ruang);
        
        $rs_vendor = $this->m_vendor->get_all_vendor();
        $this->smarty->assign("rs_vendor", $rs_vendor);

        $barang_kondisi = Array(
            Array("data" => "baik"),
            Array("data" => "rusak")
        );

        $barang_status = Array(
            Array("data" => "digunakan"),
            Array("data" => "digudang"),
            Array("data" => "disewakan"),
            Array("data" => "dipinjamkan"),
            Array("data" => "service")
        );
        $this->smarty->assign("barang_status", $barang_status);
        $this->smarty->assign("barang_kondisi", $barang_kondisi);

        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }

    public function edit_process() {
        // cek input 
        // cek input 
        $this->tnotification->set_rules('barang_noinventaris', 'No Inventaris Barang', 'trim|required|max_length[30]');
        $this->tnotification->set_rules('barang_nama', 'Nama Barang', 'trim|required|max_length[100]');
        $this->tnotification->set_rules('barang_nilai_buku', 'Nilai Buku', 'trim|required|max_length[100]');
        $this->tnotification->set_rules('barang_harga_beli', 'Harga Barang', 'trim|required|max_length[100]');
        $this->tnotification->set_rules('barang_umurekonomis', 'Umur Ekonomis', 'trim|required|max_length[2]');

        $this->tnotification->set_rules('gpbarang_cd', 'Group Barang', 'trim|required');
        $this->tnotification->set_rules('jenisbarang_cd', 'Jenis Barang', 'trim|required');
        $this->tnotification->set_rules('merk_cd', 'Merk Barang', 'trim|required');
        $this->tnotification->set_rules('kategoribarang_cd', 'Kategori Barang', 'trim|required');
        $this->tnotification->set_rules('barang_id', 'Barang ID', 'trim|required');
        //set parameter
        // process
        if ($this->tnotification->run() !== FALSE) {
            // parameter
            $barang_susut = $this->input->post('barang_susut');
            $barang_susut = empty($barang_susut) ? '0' : $barang_susut;
            $barang_garansi = $this->input->post('barang_garansi');
            $barang_garansi = empty($barang_garansi) ? '0' : $barang_garansi;

            //exit();
            //set parameter
            $params = array(
                $this->input->post('barang_noinventaris'),
                $this->input->post('barang_nama'),
                str_replace(".", "", $this->input->post('barang_harga_beli')),
                str_replace(".", "", $this->input->post('barang_nilai_buku')),
                $this->input->post('barang_umurekonomis'),
                $this->input->post('barang_keterangan'),
                $this->input->post('barang_kondisi'),
                $this->input->post('barang_status'),
                $this->input->post('barang_tglbeli'),
                null,
                $barang_garansi,
                $this->input->post('barang_tgl_garansi'),
                null,
                $barang_susut,
                $this->input->post('gpbarang_cd'),
                $this->input->post('vendor_cd'),
                $this->input->post('lokasi_cd'),
                $this->input->post('ruang_cd'),
                $this->input->post('kategoribarang_cd'),
                $this->input->post('jenisbarang_cd'),
                $this->input->post('merk_cd'),
                null,
                null,
                $this->com_user['user_id'],
                $this->input->post('barang_id')
            );

            // edit
            if ($this->m_barang->edit($params)) {
                // notification
                $this->tnotification->delete_last_field();
                $this->tnotification->sent_notification("success", "Data berhasil disimpan");
            } else {
                // default error
                $this->tnotification->sent_notification("error", "Data gagal disimpan");
            }
        } else {
            // default error
            $this->tnotification->sent_notification("error", "Data gagal disimpan");
        }
        // default redirect
        redirect("master/barang/edit/" . $this->input->post('barang_id'));
    }

    public function upgrade($id){
        // $this->_set_page_rule("R");
        // set template content
        $this->smarty->assign("template_content", "master/barang/upgrade.html");
        //data
        $result = $this->m_barang->get_barang_by_id($id); 
        if(empty($result['barang_nilai_total'])){
            $result['barang_nilai_total'] = 0;
        }
        $result_jb = $this->m_jenisbarang->get_barang_by_id(array($result['jenisbarang_cd'], $result['kategoribarang_cd']));
        $result['jenisbarang_name'] = $result_jb['jenisbarang_name'];
        
        $result_merk = $this->m_merk->get_barang_by_id(array($result['merk_cd'], $result['kategoribarang_cd']));
        $result['merk_name'] = $result_merk['merk_name'];
        
        
        $this->smarty->assign("result", $result);
        // query        
        $this->smarty->assign("kategoribarang", $this->m_kategoribarang);
        $this->smarty->assign("gpbarang", $this->m_group_barang);
        $this->smarty->assign("jenisbarang", $this->m_jenisbarang);
        $this->smarty->assign("merk", $this->m_merk);
        $this->smarty->assign("lokasi", $this->m_lokasi);
        $this->smarty->assign("ruang", $this->m_ruang);
        $this->smarty->assign("vendor", $this->m_vendor); 
        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }
    
    public function upgrade_proccess(){
        // cek input 
        $this->tnotification->set_rules('barang_id', 'Kode', 'trim|required|max_length[3]');
    }
    
    
    public function delete($id) {
        // $this->_set_page_rule("R");
        // set template content
        $this->smarty->assign("template_content", "master/barang/delete.html");
        //data
        $result = $this->m_barang->get_barang_by_id($id);
        $this->smarty->assign("result", $result);
        // query        
        $this->smarty->assign("kategoribarang", $this->m_kategoribarang);
        $this->smarty->assign("gpbarang", $this->m_group_barang);
        $this->smarty->assign("jenisbarang", $this->m_jenisbarang);
        $this->smarty->assign("merk", $this->m_merk);
        $this->smarty->assign("lokasi", $this->m_lokasi);
        $this->smarty->assign("ruang", $this->m_ruang);
        $this->smarty->assign("vendor", $this->m_vendor);
        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }

    public function delete_process() {
        // cek input 
        $this->tnotification->set_rules('barang_id', 'Kode', 'trim|required|max_length[3]');
        // process
        if ($this->tnotification->run() !== FALSE) {
            // parameter
            $params = array($this->input->post('barang_id')
            );
            // insert
            if ($this->m_barang->delete($params)) {
                // notification
                $this->tnotification->delete_last_field();
                $this->tnotification->sent_notification("success", "Data berhasil disimpan");
            } else {
                // default error
                $this->tnotification->sent_notification("error", "Data gagal disimpan");
            }
        } else {
            // default error
            $this->tnotification->sent_notification("error", "Data gagal disimpan");
        }
        // default redirect
        redirect("master/barang/");
    }

    function get_merk_by_kategoribarang_cd() {
        $kategoribarang_cd = $this->input->post('kategoribarang_cd');
        //$kategoribarang_cd = 2;
        $result = $this->m_merk->get_merk_by_kategoribarang_cd($kategoribarang_cd);
        echo json_encode($result);
    }

    function get_jenisbarang_by_kategoribarang_cd() {
        $kategoribarang_cd = $this->input->post('kategoribarang_cd');
        //$kategoribarang_cd = 2;
        $mapel = $this->m_merk->get_merk_by_kategoribarang_cd($kategoribarang_cd);
        $data = "<option value=''>--Pilih--</option>";
        foreach ($mapel as $mp) {
            $data .= "<option value='$mp[merk_cd]'>$mp[merk_name]</option>\n";
        }

        echo $kategoribarang_cd;
    }

    // import data via excel
    public function import() {
        $this->_set_page_rule("R");

        // set template content
        $this->smarty->assign("template_content", "master/barang/import.html");


        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();

        // output
        parent::display();
    }

    public function get_id() {
        $time = microtime(true);
        $id = str_replace('.', '', $time);
        return $id;
    }

    public function import_process() {
        // set page rules
        $this->_set_page_rule("C");

        // load
        $this->load->library('phpexcel');
        $this->load->library('tupload');

        $time = microtime(true);
        $id = str_replace('.', '', $time);

        // upload config
        $config['upload_path'] = 'resource/doc/temp/';
        $config['allowed_types'] = 'xls|xlsx';
        $config['file_name'] = "import_barang";
        $config['overwrite'] = TRUE;
        $this->tupload->initialize($config);
        if (!$this->tupload->do_upload('import_att')) {
            // jika gagal (kembalikan pesan)
            $this->tnotification->set_error_message($this->tupload->display_errors());
            $this->tnotification->sent_notification("error", "Data gagal disimpan");
        } else {
            $data = $this->tupload->data();
            // read data
            $file_path = 'resource/doc/temp/' . $data['file_name'];
            if (is_file($file_path)) {
                // ready to read
                $worksheet = PHPExcel_IOFactory::load($file_path);
                $sheet = $worksheet->getSheet(0);

                // init
                $i = 6;
                $cell_data = $sheet->getCell('A' . $i);
                $data = $cell_data->getValue();

                $tot_data = 0;
                $tot_insert = 0;
                while (!empty($data)) {
                    $tot_data++;
                    $gpbarang_cd = $this->m_kategoribarang->get_gpbarang_cd_by_kategoribarang_cd($sheet->getCell('J' . $i)->getValue());
                    $params = array(
                        $sheet->getCell('B' . $i)->getValue(),
                        $sheet->getCell('C' . $i)->getValue(),
                        $sheet->getCell('D' . $i)->getValue(),
                        $sheet->getCell('E' . $i)->getValue(),
                        $sheet->getCell('N' . $i)->getValue(),
                        $sheet->getCell('H' . $i)->getValue(),
                        $sheet->getCell('I' . $i)->getValue(),
                        null,
                        $gpbarang_cd,
                        $sheet->getCell('J' . $i)->getValue(),
                        $sheet->getCell('K' . $i)->getValue(),
                        $sheet->getCell('L' . $i)->getValue(),
                        null,
                        $sheet->getCell('M' . $i)->getValue(),
                        "1313"
                    );

                    $result = $this->m_barang->get_barang_by_noinventaris($sheet->getCell('B' . $i)->getValue());
                    //insert                    
                    if (empty($result)) {
                        $tot_insert++;
                        $this->m_barang->insert($params);
                    }

                    // get
                    $i++;
                    $cell_data = $sheet->getCell('A' . $i);
                    $data = $cell_data->getValue();
                }
                // delete after reading
                unlink($file_path);
            }
            // jika sukses
            $this->tnotification->sent_notification("success", $tot_insert . " Data berhasil diimport " . "dari " . $tot_data);
        }

        // default redirect
        redirect("master/barang/import");
    }
    
    /*
     * START AJAX
     */

    public function cari_jenis_barang() {
        $kategoribarang = $this->input->post('kategoribarang');
        $jenisbarang = $this->input->post('jenisbarang');

        $result = $this->m_jenisbarang->get_jenisbarang_by_kategoribarang($kategoribarang);
        $html = '<option></option>';
        foreach ($result as $key => $value) {
            $selected = "";
            if ($jenisbarang == $value['jenisbarang_cd']){
                $selected = 'selected="selected"';
            }
            $html .= '<option value="' . $value['jenisbarang_cd'] . '" '.$selected.'>' . $value['jenisbarang_cd'].' - '.$value['jenisbarang_name'] . '</option>';
        }
        echo $html;
    }

    public function cari_merk_barang() {
        $kategoribarang = $this->input->post('kategoribarang');
        $merk_cd = $this->input->post('merk_cd');

        $result = $this->m_merk->get_merk_by_kategoribarang($kategoribarang);
        $html = '<option></option>';
        foreach ($result as $key => $value) {
            $selected = "";
            if ($merk_cd == $value['merk_cd']){
                $selected = 'selected="selected"';
            }
            $html .= '<option value="' . $value['merk_cd'] . '" '.$selected.'>' . $value['merk_cd'].' - '.$value['merk_name'] . '</option>';
        }
        echo $html;
    }

    /*
     * END AJAX
     */

}
